<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/2-1'}">老师管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/2-1'}">老师列表</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/2-1'}">老师编辑</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row style="margin-top: 50px">
      <el-col :span="20" :offset="2">
        <el-form :model="tableData" ref="tableData" class="demo-ruleForm" :inline="true" :rules="rules">
          <div>
            <el-form-item label="更新头像" prop="headimg">
              <el-upload :action="upload" :on-success="handleupload" name="files"
                         :show-file-list="false">
                <el-button type="primary">点我更新</el-button>
              </el-upload>
              <img :src="tableData.headImg" class="headimg" v-show="tableData.headImg" alt="您的图片丢失了">
            </el-form-item>
          </div>
          <div>
            <el-form-item label="老师姓名" prop="name">
              <el-input v-model="tableData.name">
              </el-input>
            </el-form-item>
            <el-form-item label="老师性别" prop="sex" class="sex">
              <el-select v-model="tableData.sex" placeholder="请选择性别">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item label="微信昵称" prop="wechatNumber">
            <el-input v-model="tableData.wechatNumber">

            </el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="tableData.phone"></el-input>
          </el-form-item>
          <div>
            <el-form-item>
              <el-button type="primary" @click="wentback">返回</el-button>
              <el-button type="warning" @click="submit('tableData')">修改</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import axios from 'axios'
  import {
    teacheredit, teachercheck
  } from "@/api/teacher/teacher";

  export default {
    name: "TeacherEdit",
    props:{
      upload:String
    },
    data() {
      return {
        options: [{
          value: 1,
          label: '男'
        },
          {
            value: 2,
            label: '女'
          }
        ],
        tableData: {},
        rules: {
          name: [{
            required: true,
            message: '请输入您的姓名',
            trigger: 'blur'
          },
            {
              min: 1,
              max: 5,
              message: '名字这么长，你是日本人啊？',
              trigger: 'blur'
            }
          ],
          sex: [{
            required: true,
            message: '请选择老师的性别',
            trigger: 'change'
          }],
          wechatNumber: [{
            required: true,
            message: '请输微信账号',
            trigger: 'blur'
          },
            {
              min: 1,
              max: 20,
              message: '您的微信号也太长了',
              trigger: 'blur'
            }
          ],
          phone: [{
            required: true,
            message: '请输手机账号',
            trigger: 'blur'
          }, {
            pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }],
          img: [{
            required: true,
            message: '请添加图片',
            trigger: 'change'
          }]
        }
      }
    },
    methods: {
      handleupload(response){
        this.tableData.headImg=response.URLList[0].url;
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      wentback() {
        this.$router.push({name: 'TeachList'})
      },
      submit(tableData) {
        let params = {};
        let updateBy=JSON.parse(localStorage.userinfo).id;
        params.updateBy=updateBy;
        params.headImg=this.tableData.headImg;
        params.name = this.tableData.name;
        params.sex = this.tableData.sex;
        params.wechatNumber = this.tableData.wechatNumber;
        params.phone = this.tableData.phone;
        this.$refs[tableData].validate((valid) => {
          if (valid) {
            this.$confirm('确定修改老师信息吗?', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'success'
            }).then(() => {
              let _this = this;
              let id = _this.$route.params.index;
              teacheredit(id, params).then(function (res) {
                console.log(res);
                if (res.data.id.length !== 0) {
                  _this.$message({
                    type: 'success',
                    message: '已修改老师信息!'
                  });
                  _this.$router.push({name: 'TeachList'})
                }
              })
            })
                .catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消修改'
                });
              });
          }
          else {
            return false
          }
        })
      },
      getDetailInfo() {
        localStorage.teacherindex = this.$route.params.index === undefined ? localStorage.teacherindex : this.$route.params.index;
        let id = localStorage.teacherindex;
        teachercheck(id).then(this.handleGetDataSucc)
      }
      ,
      handleGetDataSucc(res) {
        console.log(res);
        this.tableData = res.data;
      }
    },
    mounted() {
      this.getDetailInfo();
    }
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";
  .sex {
    /deep/ .el-input__inner {
      width: 202px;
    }
  }
  .headimg{
    .headimg;
  }
</style>
